﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Estimate.Common.DBFieldTypes;
using Estimate.Common.Repository;
using Estimate.Common.Entity;
using Estimate.Common.Estimate;
using Newtonsoft.Json;

namespace Estimate.Reports
{
    /// <summary>
    /// 省台预报员排名类
    /// </summary>
    public class StIndividualRankReport
    {
        List<Forecaster> Forecasters = new List<Forecaster>();//排名结果
        private FullInfoReport memberReportScore;// 得分对象
        private List<string> SiteForeTables;//所有省台预报员数据表
        private List<string> NeedRemoveTables = new List<string>() { "_SiteFore", "Jiangsu_SiteFore", "Schedule_SiteFore", "St_SiteFore" };
        protected string _connectionString;
        ExecutionStatus exeStatus;

        /// <summary>
        /// 
        /// </summary>
        /// <param name="connectionString">数据库连接字符串</param>
        /// <param name="tid">程序执行任务号</param>
        public StIndividualRankReport(string connectionString, string tid)
        {
            _connectionString = connectionString;

            exeStatus = new ExecutionStatus(tid);
            exeStatus.SetMessage("Initial", "程序正在准备...");
            Initial();

            exeStatus.DeleteMessage("Initial");
        }

        /// <summary>
        /// 初始化
        /// </summary>
        /// <param name="beginDate"></param>
        /// <param name="endDate"></param>
        /// <param name="clocks"></param>
        /// <param name="sendpoints"></param>
        protected void Initial()
        {
            //所有省台预报员数据表名
            SiteForeTables = SiteForeRepository.GetSiteForeTables(_connectionString);
            foreach (string table in NeedRemoveTables)
            {
                if (SiteForeTables.Contains(table))
                    SiteForeTables.Remove(table);
            }
            memberReportScore = new FullInfoReport(_connectionString);
            memberReportScore.CulculateWeatherScore = true;
            memberReportScore.CulculateWaterfallLevelWaterScore = true;
            memberReportScore.CulculateWaterfallAmountWaterScore = true;
            memberReportScore.CulculateMaxTempScore = true;
            memberReportScore.CulculateMinTempScore = true;
            memberReportScore.ExeStatus = exeStatus;
        }

        public  void PrepareData(DateTime beginDate, DateTime endDate, ForeSemiType semitype)
        {
            ForeSpanType[] spans = new ForeSpanType[] { ForeSpanType.FD00_24, ForeSpanType.FD24_48 };
            ForeSendpointType[] sendpoints = new ForeSendpointType[] { ForeSendpointType.H06, ForeSendpointType.H16 };
            PrepareData(beginDate, endDate, spans, sendpoints, semitype);
        }

        public  void PrepareData(DateTime beginDate, DateTime endDate, ForeSpanType[] spans, ForeSendpointType[] sendpoints, ForeSemiType semitype)
        {

            exeStatus.TotalStep = SiteForeTables.Count * spans.Length * 2;
            exeStatus.SetMessage("memberReportScore", "得分计算程序正在运行");

            foreach (string table in SiteForeTables)
            {
                string username = table.Substring(0, table.IndexOf('_'));

                exeStatus.SetMessage(table, "当期计算的预报员为:" + username);

                memberReportScore.DataProvider = ForeRepositoryFactory.SiteForeRepository(_connectionString, username);
                memberReportScore.PrepareData(beginDate, endDate, semitype, spans, spans, sendpoints);
                Forecasters.Add(new Forecaster(username, "省台", String.Format("{0:N2}", memberReportScore.CalIndividualShortCompositeScore(""))));

                exeStatus.DeleteMessage(table);
            }

            exeStatus.DeleteMessage("memberReportScore");
        }


        /// <summary>
        /// 获取预报员得分排名
        /// </summary>
        /// <param name="top">前top名预报员得分,top=-1表示全部</param>
        /// <returns></returns>
        public string GetForecastRankResult(int top)
        {
            exeStatus.SetMessage("GetForecastRankResult", "正在获得计算结果");

            Forecasters.Sort(new IndividualCompositeScore());

            string result = "";
            
            if (top == -1 || Forecasters.Count < top)
                result= JsonConvert.SerializeObject(Forecasters);
            else
            {
                List<Forecaster> TopForecasters = new List<Forecaster>();
                for (int i = 0; i < top; i++)
                    TopForecasters.Add(Forecasters[i]);

              
                result= JsonConvert.SerializeObject(TopForecasters);
            }

           exeStatus.DeleteMessage("GetForecastRankResult");
           exeStatus.SetMessage("ExecutionEnd", "程序执行完毕");

           return result;
        }

    }


}
